package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.ITransferBizService;
import com.kk.xhr.admin.controller.request.TransferAddRequest;
import com.kk.xhr.admin.controller.request.TransferQueryRequest;
import com.kk.xhr.admin.controller.vo.TransferVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 调岗管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "调岗管理", tags = "调岗管理")
@RequestMapping("/transfer")
public class TransferController {
    private final Executor adminControllerBizExecutor;
    private final ITransferBizService transferBizService;

    @ApiOperation(value = "分页查询调岗")
    @GetMapping
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<PageVO<TransferVO>>> queryPage(@Valid TransferQueryRequest request) {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(transferBizService.queryPage(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加调岗")
    @PostMapping
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> add(@RequestBody @Valid TransferAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            transferBizService.add(request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加调岗")
    @PostMapping("/mine")
    public CompletionStage<ApiResult<Void>> addMine(@RequestBody @Valid TransferAddRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> {
            request.setUserId(userId);
            transferBizService.add(request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改调岗状态")
    @PutMapping("/status/{id:.+}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, BinaryEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            transferBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
